/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.UserDTO;
import Util.DBUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author DatPLT
 */
public class UserDAO {
    public static UserDTO checkLogin(String username, String password){
        UserDTO result = null;
        ResultSet rs = null;
        Connection conn = null;
        Statement stm = null;
        String sql;
        DBUtil util = new DBUtil();

        try {
            Class.forName(util.getDriverName());
            DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
            conn = DriverManager.getConnection(util.getURL(), util.getUsername(), util.getPassword());
            sql = "SELECT * FROM Users WHERE Username ='"+username+"' AND Password ='"+password+"'";

            stm = conn.createStatement();
            rs = stm.executeQuery(sql);
            if(rs.next()){
                result = new UserDTO(rs.getNString("Username"),rs.getNString("Password"),
                                    rs.getNString("FullName"),rs.getNString("Email"),
                                    rs.getNString("Phone"),rs.getNString("Address"));
            }
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                stm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
